package com.mxpio.boot.autoconfigure.security;

import com.mxpio.framework.common.util.BeanReflectionUtils;
import com.mxpio.framework.security.base.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Configuration
@EnableDubbo
@ConditionalOnMissingClass({"com.mxpio.boot.security.api.SecurityApiConfiguration"})
@ConditionalOnClass(UserService.class)
@AutoConfigureBefore(SecurityApiAutoConfiguration.class)
public class SecurityApiConfig {
    public SecurityApiConfig() {
        log.info("@ConditionalOnMissingClass({\"com.mxpio.boot.security.api.SecurityApiConfiguration\"})");
    }

//    @Bean
//    @DubboReference
//    public List<Object> dataColumnFilterService() {
//        List<Object> references = new ArrayList<>();
//        for (Class<?> serviceInterface : BeanReflectionUtils.packageScanner("com.mxpio.framework.security.base.service")) {
//            references.add(createReference(serviceInterface));
//        }
//        return references;
//    }

    private Object createReference(Class<?> serviceInterface) {
        ReferenceConfig<Object> reference = new ReferenceConfig<>();
        reference.setInterface(serviceInterface);
        // 设置其他通用配置，如超时、重试等
        reference.setTimeout(3000);
        reference.setRetries(2);
        // 根据需要设置更多配置
        return reference.get(); // 获取代理对象
    }

}
